// wode/bianji/bianji.js
var app=getApp()
Page({
  /**
   * 页面的初始数据
   */
  data: {
    id:wx.getStorageSync('userid'),
    img:wx.getStorageSync('userimg'),
    name:wx.getStorageSync('username'),
  },
  //选择图片
  chooseSource: function() {
    var _this = this;
    wx.showActionSheet({
      itemList: ["拍照", "从相册中选择"],
      itemColor: "#CED63A",
      success: function(res) {
        if(!res.cancel) {
          if(res.tapIndex == 0) {
            _this.imgWShow("camera")        //拍照
          } else if (res.tapIndex == 1) {
            _this.imgWShow("album")      //相册
          }
        }
      }
    })
  },
  // 点击调用手机相册/拍照
  imgWShow: function(type) {
    var _this = this;
    let len = 0;
    if(_this.data.imgList != null) {
      len = _this.data.imgList.length
    }   
    //获取当前已有的图片
    wx.chooseImage({
      count: 1,     //最多还能上传的图片数,这里最多可以上传5张
      sizeType: ['original', 'compressed'],        //可以指定是原图还是压缩图,默认二者都有
      sourceType: [type],             //可以指定来源是相册还是相机, 默认二者都有
      success: function(res) {
        wx.showToast({
          title: '正在上传...',
          icon: "loading",
          mask: true,
          duration: 1000
        })
        _this.setData({
          imagePath:res.tempFilePaths[0]
        })
         //上传图片打印文本
         wx.uploadFile({
          filePath: res.tempFilePaths[0],
          name: 'image',
          header: {
            'content-type': 'multipart/form-data'
          },
          url: 'https://www.prebeauty.work:3001/api/upload',
          success:res=>{
            console.log(res.data);
            _this.setData({
              userimg: 'https://www.prebeauty.work:3001/static/image/'+ res.data
            })
            wx.setStorageSync('userimg',  _this.data.userimg)
          }
        })
      },
      fail: function () {
        wx.showToast({
          title: '图片上传失败',
          icon: 'none'
        })
        return;
      }
    })
  },
 
//  bindInput
  userimgInput: function(e){
    this.setData({
      userimg:e.detail.value,
    })
  },
  bindKeyInput: function(e){
    this.setData({
      username:e.detail.value,
    })
  },
  useremailInput: function(e){
    this.setData({
      useremail:e.detail.value,
    })
  },
  userphoneInput: function(e){
    this.setData({
      userphone:e.detail.value,
    })
  },
  userbirthInput: function(e){
    this.setData({
      userbirth:e.detail.value,
    })
  },
  usercityInput: function(e){
    this.setData({
      usercity:e.detail.value,
    })
  },
  baocun: function(){
    if(this.data.userimg){
      wx.setStorage({
        key:"userimg",
        data:this.data.userimg,
      })}
    if(this.data.nickName){
      wx.setStorage({
        key:"nickName",
        data:this.data.nickName,
      })}
    if(this.data.useremail){
      wx.setStorage({
        key:"useremail",
        data:this.data.useremail,
      })}
    if(this.data.userphone){
      wx.setStorage({
        key:"userphone",
        data:this.data.userphone,
      })}
    if(this.data.userbirth){
      wx.setStorage({
        key:"userbirth",
        data:this.data.userbirth,
      })}
    if(this.data.usercity){
      wx.setStorage({
        key:"usercity",
        data:this.data.usercity,
      })}
      // else{
      //   wx.showModal({
      //     title:'用户名为空',
      //     content:'请输入用户名',
      //     success:function(res){
      //       if(res.confirm){
      //         console.log('用户点击确定')
      //       }
      //     }
      //   })
      // }
    },
    baocun(){
      wx.showLoading({
        title: '保存中',
        duration: 2000
      })
      wx.request({
        url: `https://www.prebeauty.work:3001/api/userpage/${this.data.id}`,
        data:{
          userimg:this.data.userimg,
          username:this.data.username,
          useremail:this.data.useremail,
          userphone:this.data.userphone,
          userbirth:this.data.userbirth,
          usercity:this.data.usercity
        },
        header:{
          'content-type':'application/json'
        },
        method:'PUT',
        success:res=>{
          wx.navigateBack({
            delta: 1,
          })
          wx.setStorageSync('username', this.data.username)
          wx.setStorageSync('userimg', this.data.userimg)
        },
      })
    },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

    this.setData({
      id:wx.getStorageSync('userid'),
      img:wx.getStorageSync('userimg'),
      name:wx.getStorageSync('username'),
    })

    wx.request({
      url: `https://www.prebeauty.work:3001/api/userpage?userid=${this.data.id}`,
      header:{
        'content-type':'application/json'
      },
      method:'GET',
      success:res=>{
        console.log(res.data)
        this.setData({
          list:res.data[0],
        })
      }
    })
  },

  // baocun(){
  //   wx.navigateTo({
  //     url: '../personalData/personalData',
  //   })
  // },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that=this;
    wx.getStorage({
      key:'userimg',
      success:function(res){
        console.log(res.data)
        that.setData({
          userimg:res.data,
        })
      }
    })
    wx.getStorage({
      key:'nickName',
      success:function(res){
        console.log(res.data)
        that.setData({
          nickName:res.data,
        })
      }
    })

    wx.getStorage({
      key:'useremail',
      success:function(res){
        console.log(res.data)
        that.setData({
          useremail:res.data,
        })
      }
    })
    wx.getStorage({
      key:'userphone',
      success:function(res){
        console.log(res.data)
        that.setData({
          userphone:res.data,
        })
      }
    })
    wx.getStorage({
      key:'userbirth',
      success:function(res){
        console.log(res.data)
        that.setData({
          userbirth:res.data,
        })
      }
    })
    wx.getStorage({
      key:'usercity',
      success:function(res){
        console.log(res.data)
        that.setData({
          usercity:res.data,
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  })